import { FitAddon } from "xterm-addon-fit";
import { connectToSocket } from "./socket";
import { useTerminal } from "./terminal";

const SERVER_ADDRESS = "http://localhost:8080";
const TERMINAL_CONTAINER = "#terminal-container";

export default function () {
  const termial = useTerminal({ cursorBlink: true });
  let fit_addon = new FitAddon();

  async function start() {
    termial.setOption("cursorStyle", "block");
    termial.setOption("theme", {
      background: "#202B33",
      foreground: "#F5F8FA",
    });
    termial.loadAddon(fit_addon);
    const container = document.querySelector(TERMINAL_CONTAINER);
    const socket = await connectToSocket(SERVER_ADDRESS);
    termial.open(container, socket);
    fit_addon.fit();
  }

  return { start };
}
